﻿// <copyright company="eCollabro">
// Copyright (c) 2014 All Rights Reserved 
// Collaborative Framework and CMS - eCollabro.com
// </copyright>
// <author>Anand Singh</author>
#region References

using System;
using System.ServiceModel;
using eCollabro.Service.DataContracts;
using eCollabro.Service.ServiceContracts;
using eCollabro.Client.ServiceProxy.Interface;
using eCollabro.Client.Models.Core;
using System.Collections.Generic;
using eCollabro.DataMapper;
using eCollabro.Service.DataContracts.Core;
using eCollabro.Utilities;

#endregion

namespace eCollabro.Client.ServiceProxy
{
    /// <summary>
    /// SetupClient
    /// </summary>
    public class SetupServiceProxy : BaseServiceProxy, ISetupServiceProxy
    {
        private ISetupService _setupProxy = null;

        public SetupServiceProxy()
        {
            ChannelFactory<ISetupService> channelFactory = new ChannelFactory<ISetupService>("BasicHttpBinding_ISetupService");
            channelFactory.Endpoint.EndpointBehaviors.Add(new eCollabroServiceBehavior());
            _setupProxy = channelFactory.CreateChannel();
            eCollabroRequest.ActiveUser = SecurityClientTranslate.Convert(base.GetUserContext());
        }


        #region Prometric.Proctoring Setup

        /// <summary>
        /// CheckPrometric.ProctoringSetup
        /// </summary>
        /// <returns></returns>
        public bool CheckEcollabroSetup()
        {
            bool setupDone = false;
            ServiceResponse<bool> checkProctoringSetupResponse = _setupProxy.CheckEcollabroSetup();
            if (checkProctoringSetupResponse.Status != ResponseStatus.Success)
            {
                HandleError(checkProctoringSetupResponse.Status, checkProctoringSetupResponse.ResponseMessage);
            }
            else
            {
                setupDone = checkProctoringSetupResponse.Result;
            }
            return setupDone;
        }

        /// <summary>
        /// Prometric.eCollabroSetup
        /// </summary>
        /// <param name="registerModel"></param>
        public void eCollabroSetup(RegisterModel registerModel)
        {
            RegisterDC registerDC = new RegisterDC();
            registerDC.UserName = registerModel.UserName;
            registerDC.Password = DataEncryption.Encrypt(registerModel.Password);
            registerDC.Email = registerModel.Email;
            ServiceResponse ProctoringSetupResponse = _setupProxy.eCollabroSetup(registerDC);
            if (ProctoringSetupResponse.Status != ResponseStatus.Success)
            {
                HandleError(ProctoringSetupResponse.Status, ProctoringSetupResponse.ResponseMessage);
            }
        }

        #endregion

        #region Email Configuration

        /// <summary>
        /// GetEmailConfiguration
        /// </summary>
        /// <returns></returns>
        public EmailConfigurationModel GetEmailConfiguration()
        {
            EmailConfigurationModel emailConfiguration = null;
            ServiceResponse<EmailConfigurationDC> emailConfigurationResponse = _setupProxy.GetEmailConfiguration();
            if (emailConfigurationResponse.Status != ResponseStatus.Success)
            {
                HandleError(emailConfigurationResponse.Status, emailConfigurationResponse.ResponseMessage);
            }
            else
            {
                emailConfiguration = Mapper.Map<EmailConfigurationDC, EmailConfigurationModel>(emailConfigurationResponse.Result);
            }
            return emailConfiguration;
        }


        /// <summary>
        /// SaveEmailConfiguration
        /// </summary>
        /// <param name="emailConfigurationModel"></param>
        public void SaveEmailConfiguration(EmailConfigurationModel emailConfigurationModel)
        {
            EmailConfigurationDC emailConfigurationDC = Mapper.Map<EmailConfigurationModel, EmailConfigurationDC>(emailConfigurationModel);
            ServiceResponse emailConfigurationResponse = _setupProxy.SaveEmailConfiguration(emailConfigurationDC);
            if (emailConfigurationResponse.Status != ResponseStatus.Success)
            {
                HandleError(emailConfigurationResponse.Status, emailConfigurationResponse.ResponseMessage);
            }
        }

        #endregion

        #region Site Collection Admin

        /// <summary>
        /// GetSiteCollectionAdmins
        /// </summary>
        /// <returns></returns>
        public List<SiteCollectionAdminModel> GetSiteCollectionAdmins()
        {
            List<SiteCollectionAdminModel> siteCollectionAdminModels = new List<SiteCollectionAdminModel>();
            ServiceResponse<List<SiteCollectionAdminDC>> userResponse = _setupProxy.GetSiteCollectionAdmins();

            if (userResponse.Status == ResponseStatus.Success)
            {
                foreach (SiteCollectionAdminDC siteCollectionAdminDC in userResponse.Result)
                {
                    siteCollectionAdminModels.Add(Mapper.Map<SiteCollectionAdminDC, SiteCollectionAdminModel>(siteCollectionAdminDC));
                }
            }
            else
            {
                HandleError(userResponse.Status, userResponse.ResponseMessage);
            }
            return siteCollectionAdminModels;
        }

        /// <summary>
        /// SaveSite
        /// </summary>
        /// <param name="siteModel"></param>
        /// <returns></returns>
        public void SaveSiteCollectionAdmin(SiteCollectionAdminModel siteCollectionAdminModel)
        {
            ServiceResponse siteCollectionAdminResponse = _setupProxy.SaveSiteCollectionAdmin(siteCollectionAdminModel.UserName);
            if (siteCollectionAdminResponse.Status != ResponseStatus.Success)
                HandleError(siteCollectionAdminResponse.Status, siteCollectionAdminResponse.ResponseMessage);
        }

        /// <summary>
        /// DeleteSiteCollectionAdmin
        /// </summary>
        /// <param name="siteId"></param>
        /// <returns></returns>
        public void DeleteSiteCollectionAdmin(int userId)
        {
            ServiceResponse siteCollectionAdminResponse = _setupProxy.DeleteSiteCollectionAdmin(userId);

            if (siteCollectionAdminResponse.Status != ResponseStatus.Success)
                HandleError(siteCollectionAdminResponse.Status, siteCollectionAdminResponse.ResponseMessage);
        }

        #endregion

    }
}
